package com.lemner.www.hongyu.Fragement;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.lemner.www.hongyu.Adapter.UnfinishorderAdater;
import com.lemner.www.hongyu.Bean.UnfinishorderBean;
import com.lemner.www.hongyu.R;
import com.lemner.www.hongyu.Service.Service;

import java.util.Arrays;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UnfinishorderFragment extends Fragment {
    private View view;
    private RecyclerView rv_unfinishorder;
    private RecyclerView.LayoutManager layoutManager;
    private UnfinishorderAdater adapter;
    private Retrofit retrofit;
    private static String BASEURL="http://www.hongyudasha.com/propertysystem/";
    private Service service;
    private int workerid,ids;
    private UnfinishorderBean.DataBean bean;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_unfinishorder, container, false);
        Bundle bundle=getArguments();
        workerid=bundle.getInt("workerid",1);
        ids= bundle.getInt("ids",1);
        initRecyclerView();
        getDate();
        return view;
    }

    private void initRecyclerView() {
        rv_unfinishorder = (RecyclerView)view.findViewById(R.id.rv_unfinishorder);
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(getActivity());
        // 设置布局管理器
        rv_unfinishorder.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rv_unfinishorder.setHasFixedSize(true);
        adapter = new UnfinishorderAdater(getActivity());
        rv_unfinishorder.setAdapter(adapter);
    }
    public void getDate(){
        int []id=new int[]{ids};
        final String communityid= Arrays.toString(id);
        retrofit=new Retrofit.Builder().baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Service service=retrofit.create(Service.class);
       Call<UnfinishorderBean>call= service.getUnfinishorder(communityid,workerid);
        call.enqueue(new Callback<UnfinishorderBean>() {
            @Override
            public void onResponse(Call<UnfinishorderBean> call, Response<UnfinishorderBean> response) {
                 bean=response.body().getData();
                 adapter.setDate(bean);
                 adapter.setId(ids,workerid);
            }

            @Override
            public void onFailure(Call<UnfinishorderBean> call, Throwable t) {
                Log.e("error", t.getMessage());
            }
        });



    }



}
